package com.kaishengit.web;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import com.kaishengit.dao.ProDao;
import com.kaishengit.entity.Product;

public class ProServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String task=request.getParameter("task");
		if("list".equals(task)){
			try {
				findAll(request,response);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else if("add".equals(task)){
			add(request,response);
		}else if("del".equals(task)){
			del(request,response);
		}else if("update".equals(task)){
			update(request,response);
			
			
		}
				
			}
	
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		ProDao dao=new ProDao();
		Product p=dao.findById(id);
		/*response.setContentType("text/xml;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		out.print("<list>");
		out.print("<date>"+p.getId()+"</date>");
		out.print("<proName>"+p.getProName()+"</proName>");
		out.print("<price>"+p.getPrice()+"</price>");
		out.print("<onsale>"+p.isOnsale()+"</onsale>");
		out.print("</list>");
		out.flush();
		out.close();*/
		JSONObject json=JSONObject.fromObject(p);
		response.setContentType("application/json;charset=UTF-8");
		String j=json.toString();
		PrintWriter out=response.getWriter();
		out.print(j);
		out.flush();
		out.close();
		
		
	}

	private void del(HttpServletRequest request, HttpServletResponse response) {
			String id=request.getParameter("id");
			ProDao dao=new ProDao();
			dao.del(id);
			try {
				request.getRequestDispatcher("show.jsp").forward(request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		request.getMethod();
		String id=request.getParameter("id");
		String proName=request.getParameter("proName");
		System.out.println(proName);
		String price=request.getParameter("price");
		String onsale=request.getParameter("onsale");
		
		Product d=new Product();
			d.setId(id);
			d.setPrice(price);
			d.setProName(proName);
			d.setOnsale(true);
			ProDao dao=new ProDao();
			dao.save(d);
		
		
		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter out=response.getWriter();
				out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
				out.print("<list>");
				out.print("<date>"+id+"</date>");
				out.print("<proName>"+proName+"</proName>");
				out.print("<price>"+price+"</price>");
				out.print("<onsale>"+onsale+"</onsale>");
				out.print("</list>");
				out.flush();
				out.close();
				
			
				/*Product d=new Product();
				d.setId(id);
				d.setPrice(Float.parseFloat(price));
				d.setProName(proName);
				d.setOnsale(true);
				dao.save(d);*/
				
		
	}

	private void findAll(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ProDao dao=new ProDao();
		List<Product> list=dao.findAll();
		/*request.getMethod();
		response.setContentType("text/xml;charset=UTF-8 ");
		PrintWriter out=response.getWriter();
			out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			out.print("<list>");
			for(Product p:list){
				out.print("<read>"+p+"</read>");
			}
			out.print("</list>");*/
		request.setAttribute("list", list);
		try {
			request.getRequestDispatcher("jsonShow.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
